<?php
/**
 * FastApp.
 * 8/18/24
 * @Author 深色的海 (wx:deepsea_0024)
 * @Contact telegram:deepsea159 
 */

namespace App\Marketing\Api\Service;

use App\Fastapp\Api\Service\Account\WalletService;
use App\Fastapp\Model\AccountWalletLog;
use App\Marketing\Model\MarketingGradient;
use App\Marketing\Model\MarketingGradientLog;

class GradientService
{

    public function getRefunded(int $id): bool
    {
        $md = MarketingGradientLog::query()->with(['gradient:id,type'])->whereNull('get_at')->findOrFail($id);
        $t['invite']['type'] = AccountWalletLog::RECHARGE;
        $t['invite']['object_type'] = AccountWalletLog::OBJ_TYPE_INVITE;
        if ($md->update(['get_at' => date('Y-m-d H:i:s')])) {
            WalletService::inc([
                'user_id' => $md->user_id,
                'amount' => $md->refunded,
                'symbol' => get_user_cache('symbol'),
                'type' => $t[$md->gradient->code]['type'],
                'object_type' => $t[$md->gradient->code]['object_type'],
                'object_id' => $id
            ]);
            return true;
        }
        return false;
    }
}